function db, options=options

end

function connect_db
	oSystem = OBJ_NEW("IDLJavaObject$STATIC$JAVA_LANG_SYSTEM", "java.lang.System")

	; Print some of java.lang.System's properties
	javaVersion = oSystem->getProperty("java.version")
	osName      = oSystem->getProperty("os.name")
	;print, javaVersion, osName

	joLDB = OBJ_NEW("IDLJavaObject$LIDARDATABASE_IDLJDBCPOSTGIS", "lidardatabase.IDLJDBCPostgis");
	joLDB->setDatabaseURL, "localhost","lidar1"
	joLDB->setCredentials, "postgres", "wed1940"
	joLDB->setTable, "points", "point_geom", "id", "tile_id"
	joLDB->connect
	return, joLDB
end

function fetch, easting, northing, height=height, intesityrange=itr, class=class, dbc=dbc, append=append, ex_columns=cols
	if NOT(keyword_set(dbc)) then dbc = connect_db()
	if (keyword_set(height)) then begin
		do_manual_height = true
	endif else begin
		height = [0,100] ;PostGIS ignores this anyway
	endelse
	box = "BOX3D(" + strtrim(easting[0],1) + " " + strtrim(northing[0],1) + " " + strtrim(height[0],1) + ", " + strtrim(easting[1],1) + " " + strtrim(northing[1],1) + " "+ strtrim(height[1],1) + ")"
	;need to explicitly filter Z as box3d does not do this (annoyingly)
	query = "select point_geom" + cols + "from points where point_geom && '" + $
			box + "'::box3d"
	if keyword_set(do_manual_height) then query += " AND z(point_geom) >= " + $
			strtrim(height[0],1) + " AND z(point_geom) <= " + strtrim(height[1],1)
	if keyword_set(itr) then query += " AND intensity >= " + strtrim(itr[0],1) + " intensity <= " + strtrim(itr[1],1)
	if keyword_set(class) then query += " AND class = " + strtrim(class, 1)
	if keyword_set(append) then query += append

	print, query
	dbc->executeQuery, query
	return, dbc->getAllPoints()
end

function fetchc, centrex, centrey, boxsize, _extra=ex
	easting = [centrex - (boxsize / 2), centrex + (boxsize / 2)]
	northing = [centrey - (boxsize / 2), centrey + (boxsize / 2)]
	return, fetch(easting, northing,  _extra=ex)
end